﻿using System.Collections.Generic;
using System.Linq;

namespace VPLink2014.Data.MesApplis
{
	public class VOUserMapper
	{

		public VOUser user;
		public VOUserMapper():base()
		{
		}

		public VOUserMapper(string userName):base()
		{
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				user = ( from u in db.VplinkEntities.Users
						 where u.LoginUser == userName
						 select new VOUser()
						 {
							 login = u.LoginUser,
							 prenom = u.Prenom,
							 nom = u.Nom,
							 departement = u.Departement,
							 direction = u.Direction,
							 service = u.Service,
							 isManager = u.IsManager
						 } ).FirstOrDefault();
			}
		}

		public List<VOApplication> GetUserApplications()
		{
			List<VOApplication> userApplications = new List<VOApplication>();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				userApplications = ( from appliUsers in db.VplinkEntities.AppliUsers
									 from applis in db.VplinkEntities.Applications
									 from user in db.VplinkEntities.Users
									 where user.LoginUser == this.user.login
									 where applis.id == appliUsers.appli
									 where user.LoginUser == appliUsers.user
									 orderby appliUsers.rang
									 select new VOApplication() { Id = applis.id, Url = applis.url, Name = applis.titre, Categorie = applis.categorie, Description = applis.description, Obligatoire = appliUsers.obligatoire, Rang = appliUsers.rang } ).ToList<VOApplication>();
			}
			return userApplications;
		}

		public void SetApplications( List<VOApplication> applications )
		{
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				User userDb = ( from User user in db.VplinkEntities.Users
								 where user.LoginUser == this.user.login
								 select user ).FirstOrDefault<User>();


				// Deleted Apps
				var deletedApps = userDb.AppliUsers.Where( i => !applications.Select( x => x.Id ).Contains( i.appli ) );
                deletedApps.ToList<AppliUser>().ForEach(x => db.VplinkEntities.AppliUsers.Remove(x));


				foreach( VOApplication application in applications )
				{
					AppliUser appliUser = ( from AppliUser appliUsers in db.VplinkEntities.AppliUsers
											where appliUsers.user == this.user.login
											where appliUsers.appli == application.Id
											select appliUsers ).FirstOrDefault<AppliUser>();

					// Add New
					Application appliDb = ( from Application appli in db.VplinkEntities.Applications
											 where appli.id == application.Id
											 select appli ).FirstOrDefault<Application>(); if( appliUser == null )
					{
						db.VplinkEntities.AppliUsers.Add( new AppliUser()
						{
							User1 = userDb,
							Application = appliDb,
							appli = application.Id,
							user = this.user.login,
							rang = application.Rang,
							obligatoire = application.Obligatoire
						} );
					}
					else
					{
						// Update existing
						appliUser.obligatoire = application.Obligatoire;
						appliUser.rang = application.Rang;
					}
				}
			}
		}
	}
}
